/*
 *  $Id$
 *  Copyright [2010] [Panxiaobo] 
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
package pxb.openid.ns;

import java.util.HashMap;
import java.util.Map;

import pxb.openid.Constants;

/**
 * 命名空间的基本类
 * 
 * @author Panxiaobo
 * 
 */
public class Base {
    private static final String NS_PREFIX = "openid.ns";

    public static String getNs(Map<String, String> map, String NS) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (NS.equals(entry.getValue()) && entry.getKey().startsWith(NS_PREFIX)) {
                return entry.getKey().substring(NS_PREFIX.length() + 1);
            }
        }
        return null;
    }

    final protected Map<String, String> map;

    /**
     * 前缀.如'openid.', 'openid.ax'
     */
    protected String prefix;

    public Base(Map<String, String> map, String ns) {
        this.map = new HashMap<String, String>();
        if (Constants.OPENID_20_NAMESPACE.equals(ns)) {
            prefix = "openid.";
        } else {
            String x = getNs(map, ns);
            if (x == null)
                throw new RuntimeException();
            prefix = "openid." + x + ".";
            this.map.put("openid.ns." + x, ns);
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getKey().startsWith(prefix)) {
                this.map.put(entry.getKey(), entry.getValue());
            }
        }
    }

    public Base(String fullNs) {
        this(fullNs, null);
    }

    public Base(String fullNs, String shortNs) {
        this.map = new HashMap<String, String>();
        if (Constants.OPENID_20_NAMESPACE.equals(fullNs)) {
            prefix = "openid.";
            map.put("openid.ns", fullNs);
        } else {
            if (shortNs == null) {
                shortNs = "x" + (fullNs.hashCode() & 0x7FFFFFFF);
            }
            prefix = "openid." + shortNs + ".";
            map.put("openid.ns." + shortNs, fullNs);
        }
    }

    /**
     * 获取当前命名空间下的值
     * 
     * @param name
     * @return
     */
    public String get(String name) {
        return map.get(prefix + name);
    }

    /**
     * 获取数据
     * 
     * @return
     */
    public Map<String, String> getMap() {
        return map;
    }

    /**
     * 设置当前命名空间下的值
     * 
     * @param name
     * @param value
     * @return this
     */
    public Base put(String name, String value) {
        map.put(prefix + name, value);
        return this;
    }

    /**
     * 移除当前命名空间下的值
     * 
     * @param name
     * @return this
     */
    public Base remove(String name) {
        map.remove(prefix + name);
        return this;
    }
}
